package com.huishu.user.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.pojo.Result;
import com.huishu.commons.utlis.BeanUtils;
import com.huishu.user.pojo.dto.HomeDataDTO;
import com.huishu.user.pojo.dto.PageDataDTO;
import com.huishu.user.pojo.model.HomeImageUrlDO;
import com.huishu.user.pojo.model.HomePageDO;
import com.huishu.user.pojo.vo.PageDataVO;
import com.huishu.user.service.HomeImageUrlService;
import com.huishu.user.service.HomePageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author generator@ljj
 * @since 2023-04-14
 */
@Slf4j
@RestController
@RequestMapping("/home-page")
public class HomePageController {

    @Autowired
    private HomePageService homePageService;

    @Autowired
    private HomeImageUrlService homeImageUrlService;

    @PostMapping("/homeData")
    public Result homeData(@RequestBody HomeDataDTO homeDataDTO) {
        return homePageService.homeData(homeDataDTO);
    }


    @PostMapping("/create")
    public Result create(@RequestBody List<PageDataDTO> pageDatas) {
        return homePageService.create(pageDatas);
    }

    @Transactional
    @PostMapping("/update/{classify}")
    public Result modify(@RequestBody List<PageDataDTO> pageDatas, @PathVariable Integer classify) {
        log.info("修改首页参数：{}", JSON.toJSONString(pageDatas));
        // 删除
        LambdaQueryWrapper<HomePageDO> queryWrapper = Wrappers.lambdaQuery(HomePageDO.class)
                .eq(HomePageDO::getClassify, classify);
        List<HomePageDO> list = homePageService.list(queryWrapper);
        homePageService.remove(queryWrapper);
        List<Long> ids = list.stream().map(HomePageDO::getId).collect(Collectors.toList());
        LambdaQueryWrapper<HomeImageUrlDO> queryWrapper1 = Wrappers.lambdaQuery(HomeImageUrlDO.class)
                .in(HomeImageUrlDO::getHomePageId, ids);
        homeImageUrlService.remove(queryWrapper1);
        return homePageService.create(pageDatas);
    }

    @GetMapping("/find/{classify}")
    public Result find(@PathVariable Integer classify) {
        LambdaQueryWrapper<HomePageDO> queryWrapper = Wrappers.lambdaQuery(HomePageDO.class)
                .eq(HomePageDO::getClassify, classify)
                .orderByAsc(HomePageDO::getSort);
        List<HomePageDO> list = homePageService.list(queryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Result.success();
        }
        List<Long> ids = list.stream().map(HomePageDO::getId).collect(Collectors.toList());
        LambdaQueryWrapper<HomeImageUrlDO> queryWrapper1 = Wrappers.lambdaQuery(HomeImageUrlDO.class)
                .in(HomeImageUrlDO::getHomePageId, ids)
                .orderByAsc(HomeImageUrlDO::getSort);
        List<HomeImageUrlDO> list1 = homeImageUrlService.list(queryWrapper1);
        Map<Long, List<HomeImageUrlDO>> collect = list1.stream().collect(Collectors.groupingBy(HomeImageUrlDO::getHomePageId));
        List<PageDataVO> collect1 = list.stream().map(homePageDO -> {
            PageDataVO pageDataVO = BeanUtils.copyProperties(homePageDO, PageDataVO.class);
            pageDataVO.setHomeImageUrl(collect.get(homePageDO.getId()));
            return pageDataVO;
        }).collect(Collectors.toList());
        return Result.success(collect1);
    }
}
